package xcmg.device.web.basic;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.basic.DataDictionaryService;
import xcmg.device.service.vo.basic.DataDictionaryVO;
import xcmg.device.service.vo.basic.DictionaryRelationVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;
import java.util.List;

/**
 * Created by GYJ on 2017-12-27.
 */
@RequestMapping("/dataDictionary")
@RestController
public class DataDictionaryController extends BasicController{
    @Autowired
    private DataDictionaryService dataDictionaryService;
    @Value("${xcmg.version}")
    private String version;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<DataDictionaryVO> condition){

        PageCommonVO result=dataDictionaryService.list(condition);
        return actionValueResult(result.getPageInfo());
    }

    @RequestMapping(value = "/listAll",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult listAll(@RequestBody SearchCommonVO<DictionaryRelationVO> condition){

        PageCommonVO result=dataDictionaryService.listAll(condition);
        return actionValueResult(result.getPageInfo());
    }

    @RequestMapping(value = "/searchList",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult searchList(@RequestBody DictionaryRelationVO condition)throws Exception{
        condition.setOrgId(getOrgId());
        List<DictionaryRelationVO> voList = dataDictionaryService.searchList(condition);
        return actionValueResult(voList);
    }

    @RequestMapping(value = "/searchListPage",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult searchListPage(@RequestBody SearchCommonVO<DictionaryRelationVO> condition)throws Exception{
        condition.getFilters().setOrgId(getOrgId());
        return actionValueResult(dataDictionaryService.searchListPage(condition));
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.SystemManage.PARAM_ADD)
    public ActionResult add(@RequestBody DataDictionaryVO model){

        model.setCreateId(getUserId());
        ErrorCode ret=dataDictionaryService.add(model);
        return actionResult(ret);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.SystemManage.PARAM_EDIT)
    public ActionResult update(@RequestBody DataDictionaryVO model){
        if(model==null){
            return actionResult(ErrorCode.IllegalArument);
        }

        model.setUpdateId(getUserId());
        ErrorCode ret=dataDictionaryService.update(model);
        return actionResult(ret);
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult remove(@RequestBody String[] ids){
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode ret=dataDictionaryService.remove(Arrays.asList(ids));
        return actionResult(ret);
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult item(@RequestBody String id){
        if(StringUtils.isEmpty(id)){
            return actionResult(ErrorCode.IllegalArument);
        }
        DataDictionaryVO result=dataDictionaryService.item(id);
        return actionValueResult(result);
    }

    @RequestMapping(value = "/version",method = RequestMethod.GET)
    public String version(){
        return version;
    }
}
